package com.xiubai.appforpda.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xiubai.appforpda.R;
import com.xiubai.appforpda.adapter.CheckAssetsAdapter;
import com.xiubai.appforpda.db.DatabaseUtils;
import com.xiubai.appforpda.entity.SQLOrgPersonBean;
import com.xiubai.appforpda.entity.SQLPamInven2Bean;
import com.xiubai.appforpda.entity.SQLPamInvenBean;
import com.xiubai.appforpda.entity.TestCheckAssetsBean;
import com.xiubai.appforpda.event.CheckDataChangeEvent;
import com.xiubai.appforpda.event.CloseEvent;
import com.xiubai.appforpda.jack.utils.AppConstants;
import com.xiubai.appforpda.jack.utils.Trace;
import com.xiubai.appforpda.utils.ClickUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CheckAssetsActivity extends BaseActivity implements View.OnClickListener {


    private TextView tv_name;
    private View btn_check_back;
    private ListView lv_content_check;
    private CheckAssetsAdapter checkAssetsAdapter;
    ArrayList<TestCheckAssetsBean> checkAssetsBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_assets);
        EventBus.getDefault().register(this);
        initView();
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CheckDataChangeEvent event) {
        if(event != null && !TextUtils.isEmpty(event.bill_code)) {
            if(checkAssetsAdapter != null && checkAssetsBeans != null) {
                for(TestCheckAssetsBean bean : checkAssetsBeans) {
                    if(event.bill_code.equals(bean.checkCode)) {
                        bean.my_status = "Y";

                        checkAssetsAdapter.notifyDataSetChanged();
                    }
                }
            }
        }
        /* Do something */
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initData() {
        if (AppConstants.ASSET_DEPT_CODE.equals(AppConstants.DEPT_CODE)) {
            // 加载所有
            loadAllData();
        } else {
            // 加载本部门
            loadMydata();
        }

    }

    /**
     * 加载本部门
     */
    private void loadMydata() {
        List<SQLOrgPersonBean> list = DatabaseUtils.getHelper().queryByArgs(SQLOrgPersonBean.class, "deptcode", AppConstants.DEPT_CODE);
        if (null == list) {
            Toast.makeText(this, "没有查询到相应的部门", Toast.LENGTH_SHORT).show();
            return;
        }

        List<SQLPamInvenBean> beans = DatabaseUtils.getHelper().queryAll(SQLPamInvenBean.class);
        if (beans != null) {
            for (SQLPamInvenBean child : beans) {
                if (AppConstants.DEPT_CODE.equals(child.deptcode)) {
                    TestCheckAssetsBean checkAssetsBean = new TestCheckAssetsBean();
                    checkAssetsBean.checkCode = child.billcode;
                    checkAssetsBean.checkName = child.memo;
                    checkAssetsBean.checkDepartment = child.deptcode;
                    checkAssetsBeans.add(checkAssetsBean);
                }
            }
        }
        checkAssetsAdapter.notifyDataSetChanged();
        if(checkAssetsBeans.isEmpty()) {
            Toast.makeText(this, "没有查询到相应的数据", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 加载所有
     */
    private void loadAllData() {
//        List<SQLPamInvenBean> beans = DatabaseUtils.getHelper().queryAll(SQLPamInvenBean.class);
        // 多增加了一列,在读取的时候第一列读id 会跳过,导致第一列数据没读出来,所有多加了一列
        String sql = "select a.billcode, a.billcode billcode,a.memo memo,a.deptcode deptcode,b.deptname deptname from  com_xiubai_appforpda_entity_SQLOrgStructureBean b  left join com_xiubai_appforpda_entity_SQLPamInvenBean a where a.deptcode = b.deptcode";
        List<SQLPamInven2Bean>  beans = DatabaseUtils.getHelper().queryAllSql(SQLPamInvenBean.class.getName(),SQLPamInven2Bean.class,sql);
        if (beans != null) {
            for (SQLPamInven2Bean child : beans) {
                TestCheckAssetsBean checkAssetsBean = new TestCheckAssetsBean();
                checkAssetsBean.deptname = child.deptname;

                checkAssetsBean.checkCode = child.billcode;
                checkAssetsBean.checkName = child.memo;
                checkAssetsBean.checkDepartment = child.deptcode;
                child.my_status = checkAssetsBean.my_status = "Y".equals(child.my_status) ? "Y" : "N";
                checkAssetsBeans.add(checkAssetsBean);
            }
        }
        checkAssetsAdapter.notifyDataSetChanged();
    }

    private void enterMainCheckAssets(TestCheckAssetsBean item) {
        Intent intent = new Intent(this, InnerCheckAssetsActivity.class);
        intent.putExtra(AppConstants.BILL_CODE, item.checkCode);
        intent.putExtra(AppConstants.BILL_STATUS, item.my_status);
        startActivity(intent);
    }

    private void initView() {
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_name.setText("经办人: " + AppConstants.USER_NAME);
        lv_content_check = (ListView) findViewById(R.id.lv_content_check);
        btn_check_back = findViewById(R.id.btn_check_back);
        btn_check_back.setOnClickListener(this);

        checkAssetsAdapter = new CheckAssetsAdapter(this, checkAssetsBeans);
        lv_content_check.setAdapter(checkAssetsAdapter);
        lv_content_check.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                TestCheckAssetsBean item = (TestCheckAssetsBean) adapterView.getAdapter().getItem(position);
                if (item == null) {
                    return;
                }
                enterMainCheckAssets(item);
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (ClickUtil.isFastDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_check_back:
                finish();
                break;
        }
    }
}
